import { request } from '@/utils/request';

const BASE_URL = 'http://192.168.10.200';

/**
 * 获取用户列表
 * @param params 
 * @returns 
 */
export async function getUserList(params?: {
    keyword?: string;
    type?: string;
    follow_time?: string;
}) {
    const res = await request({
        url: `${BASE_URL}/marketapi/index/getConversationList`,
        params,
    });

    if (res.status) {
        const data = res.data as Record<string, any>;

        if (data['list'] && Array.isArray(data['list'])) {
            return data['list'] as Record<string, any>[];
        }
    }

    return [];
}

// 获取与用户对话的聊天内容
export async function getMessage(params: Record<string, any>) {
    const res = await request({
        url: `${BASE_URL}/marketapi/index/getMessageChatLog`,
        params,
    });

    if (res.status) {
        const data = res.data as Record<string, any>;

        if (data['list'] && Array.isArray(data['list'])) {
            return data['list'] as Record<string, any>[];
        }
    }
    return [];
}

// 设置用户备注
export async function setUserRemark(data: Record<string, any>) {
    const res = await request({
        url: `${BASE_URL}/marketapi/index/setUserConversationRemarks`,
        method: 'POST',
        data,
    });

    if (res.status) {
        const data = res.data as Record<string, any>;
        return data;
    }

    return {};
}

/**
 *  获取话术方法
 * @returns
 */
export async function getJargon() {
	// 创建异步等待的变量 调用了request方法
	const res = await request({
		url: `${BASE_URL}/marketapi/index/getScriptList`,
	})

	if (res.status) {
		// 将res.data赋值给变量data 且变量的类型是key为string,value是any
		const data = res.data as Record<string, any>
		// Array.isArray()用于判断是否为是一个数组
		if (data["list"] && Array.isArray(data["list"])) {
			// 是的话就返回 类型是一个数组且每一个元素的key为string,value是any
			return data["list"] as Record<string, any>[]
		}
	}
	// 这里为了上面的if语句 没有返回值 而导致的报错 所以在最后返回一个空数组
	return []
}

/**
 * 设置用户类型
 * @param data 
 * @returns 
 */
export async function setUserType(data: Record<string, any>) {
    const res = await request({
        url: `${BASE_URL}/marketapi/index/setUserConversationType`,
        method: 'POST',
        data,
    });

    if (res.status) {
        const data = res.data as Record<string, any>;
        return data;
    }

    return {};
}

/**
 * 发送消息
 * @param data 
 * @returns 
 */
export async function sendMessage(data: Record<string, any>) {
	const res = await request({
		url: `${BASE_URL}/marketapi/index/sendMessage`,
		method: "POST",
		data,
	})

	return res.status
}

/**
 * 跟进更新
 * @param data 
 * @returns 
 */
export async function upFollowUpDate(data: Record<string, any>) {
    const res = await request({
        url: `${BASE_URL}/marketapi/index/setUserConversationFollow`,
        method: 'POST',
        data,
    });

    return res.status;
}
